/* 用户信息 */
import { isObject, isArray, isKey } from '@/utils/type.js'

const store = {
  state: {
    token: uni.getStorageSync("x-token") || '',   // token
		userInfo: uni.getStorageSync("x-userInfo") || {}, // 用户信息
    options: uni.getStorageSync("x-options") || {}, // 配置信息
  },
  getters: {
  
    /**
     * 是否登录
     * @returns {Boolean} 登录状态
     */
    isLogin: (state) => !!state.token,
    
    /**
     * 获取用户信息 
     * @returns 直接返回全部数据
     */
    userInfo(state) {
      return state.userInfo || {};
    },
    
		/**
		 * 获取用户信息
		 * @param {String} key 要获取的信息，不传返回全部
		 * @returns 返回一个函数
		 */
    getUserInfo: (state) => (key) => {
			return key ? state.userInfo[key] : state.userInfo;
    },

		/**
		 * 获取配置信息
		 * @param {String} key 要获取的信息，不传返回全部
		 * @returns { any }  配置信息
		 */
    getOptions: (state) => (key) => {
			return key ? state.options[key] : state.options;
    },
    
    // 获取token
    token(state) {
      return state.token;
    }

  },
  mutations: {
		
    /**
     * 设置用户信息，使用assign合并
     * @param {*} state 
     * @param {Object} info 用户信息
     */
    setUserInfo: (state, info) => {
			if(!isObject(info)) {
        console.error('请传入对象用户信息！');
				return;
			}
			const userInfo = Object.assign({}, state.userInfo, info);
			state.userInfo = userInfo;
      uni.setStorageSync("x-userInfo", userInfo);
    },
		
    /**
     * 删除用户信息
     * @param {*} state 
     * @param {any} keys 要删除的key    （数组|键名|不传）
     */
		removeUserInfo: (state, keys) => {
      if(isArray(keys)) {
        keys.forEach(key => delete state.userInfo[key]);
        uni.setStorageSync("x-userInfo", state.userInfo);
      } else if(isKey(keys)){
        delete state.userInfo[keys];
        uni.setStorageSync("x-userInfo", state.userInfo);
      } else {
        state.userInfo = {};
        uni.removeStorageSync("x-userInfo");
      }
		},

    /**
     * 设置配置，使用assign合并
     * @param {*} state 
     * @param {Object} info 用户信息
     */
    setOptions: (state, option = {}) => {
			if(!isObject(option)) {
        console.error('请传入对象配置信息！');
				return;
			}
			const options = Object.assign({}, state.options, option);
			state.options = options;
      uni.setStorageSync("x-options", options);
    },
		
    /**
     * 删除配置信息
     * @param {*} state 
     * @param {any} keys 要删除的key    （数组|键名|不传）
     */
		removeOptions: (state, keys) => {
      if(isArray(keys)) {
        keys.forEach(key => delete state.options[key]);
        uni.setStorageSync("x-options", state.options);
      } else if(isKey(keys)){
        delete state.options[keys];
        uni.setStorageSync("x-options", state.options);
      } else {
        state.options = {};
        uni.removeStorageSync("x-options");
      }
		},
    
    /**
     * 设置token
     * @param {*} state 
     * @param {string} 新的token
     */
    setToken: (state, token) => {
      state.token = token;
      uni.setStorageSync("x-token", token);
    },
    
    /**
     * 删除token
     * @param {*} state 
     */
    removeToken: (state) => {
      state.token = '';
      uni.removeStorageSync("x-token");
    },

  },
  actions: {},
};
export default store;
